package io.gitee.baicaixiaozhan.httpmessagewrapper.interceptor;

import io.gitee.baicaixiaozhan.httpmessagewrapper.kit.ServletResponseResultKit;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * DESC:
 *
 * @author baicaixiaozhan
 * @since v1.0.0
 */
public class ServletResponseResultKitInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            ServletResponseResultKit.setHttpServletResponse(response);
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response,
                                Object handler,
                                Exception ex) throws Exception {
        if (handler instanceof HandlerMethod) {
            ServletResponseResultKit.clear();
        }
    }
}
